package com.getpebble.android.main.sections.support.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.getpebble.android.PebbleApplication;
import com.getpebble.android.R;
import com.getpebble.android.common.core.trace.Analytics.MobileAppBehavior;
import com.getpebble.android.common.framework.fragment.PblBaseFragment;
import com.getpebble.android.common.model.FrameworkState;
import com.getpebble.android.common.model.FrameworkState.EventType;
import com.getpebble.android.framework.FrameworkEventReceiver;
import com.getpebble.android.framework.FrameworkEventReceiver.IFrameworkEventListener;
import com.getpebble.android.framework.PblFrameworkInterface;
import com.getpebble.android.framework.notification.NotificationSender.NotificationDemoType;
import com.getpebble.android.onboarding.activity.OnboardingActivity;
import com.getpebble.android.onboarding.fragment.SetupCompleteFragment;

public class NotificationDemoFragment extends PblBaseFragment {
    public static final String TAG = NotificationDemoFragment.class.getSimpleName();
    private TextView mBanner;
    private Button mContinueButton;
    private Button mEmailButton;
    private PblFrameworkInterface mFrameworkInterface;
    private IFrameworkEventListener mFrameworkListener;
    private boolean mIsOnboarding = false;
    private Button mPhoneButton;
    private Button mSmsButton;

    public int getLayoutId() {
        return R.layout.fragment_notification_demo;
    }

    public void init(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        MobileAppBehavior.logScreenOpened("NotificationDemo");
        this.mBanner = (TextView) container.findViewById(R.id.onboarding_banner);
        this.mSmsButton = (Button) container.findViewById(R.id.btn_demo_sms);
        this.mPhoneButton = (Button) container.findViewById(R.id.btn_demo_phone);
        this.mEmailButton = (Button) container.findViewById(R.id.btn_demo_email);
        this.mContinueButton = (Button) container.findViewById(R.id.btn_continue);
        PebbleApplication.bindToFrameworkService();
        this.mFrameworkInterface = PebbleApplication.getFrameworkInterface();
        this.mSmsButton.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                MobileAppBehavior.logButtonTapped("text", "NotificationDemo");
                NotificationDemoFragment.this.sendDemoNotification(NotificationDemoType.SMS);
            }
        });
        this.mPhoneButton.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                MobileAppBehavior.logButtonTapped("phone", "NotificationDemo");
                NotificationDemoFragment.this.sendDemoNotification(NotificationDemoType.PHONE_CALL);
            }
        });
        this.mEmailButton.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                MobileAppBehavior.logButtonTapped("email", "NotificationDemo");
                NotificationDemoFragment.this.sendDemoNotification(NotificationDemoType.EMAIL);
            }
        });
        this.mFrameworkListener = new IFrameworkEventListener() {
            public void onFrameworkStateChanged(FrameworkState newState) {
                if (newState.getLastEvent() != null && newState.getLastEvent().equals(EventType.NOTIFICATION_DEMO_COMPLETE)) {
                    Activity activity = NotificationDemoFragment.this.getActivity();
                    if (!newState.isNotificationDemoSuccess()) {
                        Toast.makeText(activity, "Notification failed to send.", 1).show();
                    } else if (activity != null) {
                        Toast.makeText(activity, R.string.notification_sent, 1).show();
                    }
                }
            }
        };
        FrameworkEventReceiver.registerFrameworkStateEventListener(this.mFrameworkListener);
    }

    public void onResume() {
        super.onResume();
        handleArguments();
        if (this.mIsOnboarding) {
            this.mBanner.setVisibility(0);
            this.mContinueButton.setVisibility(0);
            this.mContinueButton.setOnClickListener(new OnClickListener() {
                public void onClick(View view) {
                    MobileAppBehavior.logButtonTapped("continue", "NotificationDemo");
                    NotificationDemoFragment.this.goToNextFragment();
                }
            });
        }
    }

    private void goToNextFragment() {
        Activity activity = getActivity();
        if (activity != null && (activity instanceof OnboardingActivity)) {
            ((OnboardingActivity) activity).switchScreenFragment(new SetupCompleteFragment());
        }
    }

    public void handleArguments() {
        Bundle args = getArguments();
        if (args != null && args.containsKey("extra_is_onboarding")) {
            this.mIsOnboarding = args.getBoolean("extra_is_onboarding");
        }
    }

    public void onDestroyView() {
        FrameworkEventReceiver.unregisterFrameworkStateEventListener(this.mFrameworkListener);
        super.onDestroyView();
    }

    private void sendDemoNotification(NotificationDemoType type) {
        this.mFrameworkInterface.sendDemoNotification(type);
    }
}
